home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1996 #6 / Amiga Plus CD - 1996 - No. 06.iso / pd / programmierung / proasm / routines / memory.r < prev    next >
Text File  |  1993-05-25  |  5KB  |  295 lines

  1.  
  2. ;---;  memory.r  ;--------------------------------------------------------------
  3. *
  4. *    ****    MEMORY HANDLING ROUTINES    ****
  5. *
  6. *    Author        Stefan Walter
  7. *    Version        1.01
  8. *    Last Revision    04.09.92
  9. *    Identifier    mem_defined
  10. *       Prefix        mem_    (memory)
  11. *                 ¯¯¯
  12. *    Functions    AllocMemory, FreeMemory, FreeAllMemory, EnchainMemory
  13. *            (AllocEMemory, FreeEMemory, FreeEAllMemory)
  14. *
  15. *    Flags        mem_SMARTASS set 1 if the additional routines required
  16. *
  17. ;------------------------------------------------------------------------------
  18.  
  19. ;------------------
  20.     ifnd    mem_defined
  21. mem_defined    =1
  22.  
  23. ;------------------
  24. mem_oldbase    equ __base
  25.     base    mem_base
  26. mem_base:
  27.  
  28. ;------------------
  29.     ifd    mem_SMARTASS
  30.  
  31. ;------------------
  32.  
  33. ;------------------------------------------------------------------------------
  34. *
  35. * AllocEMemory    Allocate memory and link it to a given list list of memory
  36. *        blocks.
  37. *
  38. * INPUT:    d0    Number of bytes to allocate
  39. *        d1    Requirements
  40. *        a0    Pointer to anchor containing the list pointer
  41. *
  42. * RESULT:    d0    Address of memory or zero if not available
  43. *
  44. ;------------------------------------------------------------------------------
  45.  
  46. ;------------------
  47. AllocEMemory:
  48.  
  49. ;------------------
  50. ; Set anchor, Allocate and reload anchor.
  51. ;
  52. \start:
  53.     pea    AllocMemory(pc)
  54.     bsr    mem_MultiList
  55.     addq.l    #4,sp
  56.     rts
  57.  
  58. mem_MultiList:
  59.     move.l    a1,-(sp)
  60.     lea    mem_anchor(pc),a1
  61.     move.l    (a1),-(sp)
  62.     move.l    (a0),(a1)
  63.     pea    \return
  64.     move.l    -16(sp),-(sp)
  65.  
  66. \return:
  67.     move.l    (a1),(a0)
  68.     move.l    (sp)+,(a1)
  69.     move.l    (sp)+,a1
  70.     rts
  71.  
  72. ;------------------
  73.     
  74. ;------------------------------------------------------------------------------
  75. *
  76. * FreeEMemory    Free a block of memory in a given list.
  77. *
  78. * INPUT:    d0    Address of block
  79. *        a0    Pointer to anchor containing list
  80. *
  81. * RESULT:    d0    0 if not existing, -1 if done
  82. *
  83. ;------------------------------------------------------------------------------
  84.  
  85. ;------------------
  86. FreeEMemory:
  87.  
  88. ;------------------
  89. ; Set anchor, Free and reload anchor.
  90. ;
  91. \find:
  92.     pea    FreeMemory(pc)
  93.     bsr    mem_MultiList
  94.     addq.l    #4,sp
  95.     rts
  96.  
  97. ;------------------
  98.  
  99. ;------------------------------------------------------------------------------
  100. *
  101. * FreeEAllMemory    Free all memory in given list.
  102. *
  103. * INPUT:    a0    Pointer to anchor containing list
  104. *
  105. ;------------------------------------------------------------------------------
  106.  
  107. ;------------------
  108. FreeEAllMemory:
  109.  
  110. ;------------------
  111. ; Set anchor, Free and reload anchor.
  112. ;
  113. \start:
  114.     pea    FreeAllMemory(pc)
  115.     bsr    mem_MultiList
  116.     addq.l    #4,sp
  117.     rts
  118.  
  119. ;------------------
  120.     endif
  121.  
  122. ;------------------
  123.  
  124. ;------------------------------------------------------------------------------
  125. *
  126. * AllocMemory    Allocate memory and link it to a list of memory blocks.
  127. *
  128. * INPUT:    d0    Number of bytes to allocate
  129. *        d1    Requirements
  130. *
  131. * RESULT:    d0    Address of memory or zero if not available
  132. *
  133. ;------------------------------------------------------------------------------
  134.  
  135. ;------------------
  136. AllocMemory:
  137.  
  138. ;------------------
  139. ; Allocate length+8 for link.
  140. ;
  141. \start:
  142.     movem.l    d1-a6,-(sp)
  143.     move.l    4.w,a6
  144.     addq.l    #8,d0
  145.     move.l    d0,d7
  146.     jsr    -198(a6)
  147.     tst.l    d0
  148.     beq.s    \done
  149.  
  150. ;------------------
  151. ; Link to list.
  152. ;
  153. \link:
  154.     move.l    d7,d1
  155.     bsr    EnchainMemory
  156.     addq.l    #8,d0
  157.  
  158. ;------------------
  159. ; Memory allocated or not.
  160. ;
  161. \done:
  162.     tst.l    d0
  163.     movem.l    (sp)+,d1-a6
  164.     rts
  165.  
  166. ;------------------
  167.  
  168. ;------------------------------------------------------------------------------
  169. *
  170. * FreeMemory    Free a block of memory.
  171. *
  172. * INPUT:    d0    Address of block
  173. *
  174. * RESULT:    d0    0 if not existing, -1 if done
  175. *
  176. ;------------------------------------------------------------------------------
  177.  
  178. ;------------------
  179. FreeMemory:
  180.  
  181. ;------------------
  182. ; Find node and free it.
  183. ;
  184. \find:
  185.     movem.l    d1-a6,-(sp)
  186.     lea    mem_anchor(pc),a1
  187.  
  188. \loop:
  189.     move.l    a1,a0
  190.     move.l    (a1),d1
  191.     beq.s    \error
  192.     move.l    d1,a1
  193.     addq.l    #8,d1
  194.     cmp.l    d0,d1
  195.     bne.s    \loop
  196.  
  197. \found:
  198.     move.l    (a1),(a0)    ;unlink
  199.     move.l    4(a1),d0
  200.     move.l    4.w,a6
  201.     jsr    -210(a6)
  202.     moveq    #-1,d0
  203.     bra.s    \done
  204.  
  205. \error:
  206.     moveq    #0,d0
  207.  
  208. ;------------------
  209. ; Memory freed or not.
  210. ;
  211. \done:
  212.     movem.l    (sp)+,d1-a6
  213.     rts
  214.  
  215. ;------------------
  216.  
  217. ;------------------------------------------------------------------------------
  218. *
  219. * FreeAllMemory        Free all memory in list.
  220. *
  221. ;------------------------------------------------------------------------------
  222.  
  223. ;------------------
  224. FreeAllMemory:
  225.  
  226. ;------------------
  227. ; Free all in list.
  228. ;
  229. \free:
  230.     movem.l    d0-a6,-(sp)
  231.     move.l    4.w,a6
  232.     move.l    mem_anchor(pc),a5
  233.     bra.s    \next
  234. \loop:
  235.     move.l    a5,a1
  236.     move.l    (a5),a5
  237.     move.l    4(a1),d0
  238.     jsr    -210(a6)
  239. \next:
  240.     move.l    a5,d0
  241.     bne.s    \loop
  242.  
  243. \done:
  244.     lea    mem_anchor(pc),a5
  245.     clr.l    (a5)
  246.     movem.l    (sp)+,d0-a6
  247.     rts
  248.  
  249. ;------------------
  250.  
  251. ;------------------------------------------------------------------------------
  252. *
  253. * EnchainMemory        Link a memory block at head of our structure.
  254. *
  255. * INPUT:    d0    Address of block
  256. *        d1    Length including structure length (8)
  257. *
  258. ;------------------------------------------------------------------------------
  259.  
  260. ;------------------
  261. EnchainMemory:
  262.  
  263. ;------------------
  264. ; Link Memory in list.
  265. ;
  266. \link:
  267.     movem.l    a4/a5,-(sp)
  268.     move.l    d0,a4
  269.     lea    mem_anchor(pc),a5
  270.     move.l    (a5),(a4)
  271.     move.l    d0,(a5)
  272.     move.l    d1,4(a4)
  273.     movem.l    (sp)+,a4/a5
  274.     rts
  275.     
  276. ;------------------
  277.  
  278. ;--------------------------------------------------------------------
  279.  
  280. ;------------------
  281. mem_anchor:    dc.l    0    ;the anchor
  282.  
  283. ;------------------
  284.  
  285. ;--------------------------------------------------------------------
  286.  
  287. ;------------------
  288.     base    mem_oldbase
  289.  
  290. ;------------------
  291.     endif
  292.  
  293.  end
  294.  
  295.